package com.liuxu.topnews.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.liuxu.topnews.BuildConfig;
import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.R;
import com.liuxu.topnews.bean.App;
import com.liuxu.topnews.utils.ShareUtil;
import com.liuxu.topnews.utils.ToastUtils;

import java.util.List;

import static com.liuxu.topnews.MyApplication.checkBoxList;
import static com.liuxu.topnews.MyApplication.checklist;
import static com.liuxu.topnews.MyApplication.globalapps;


public class Recycle_Home_Adapter extends RecyclerView.Adapter<Recycle_Home_Adapter.MyHolder> {
    Context context;

    public Recycle_Home_Adapter(){
        this.context=MyApplication.getContext();
    }

    public void update(){
        notifyDataSetChanged();
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view=null;
        switch (BuildConfig.version) {
            case "normal":
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.homeapp_item,viewGroup,false);
                break;
            case "hw":
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.homeapp_item_hw,viewGroup,false);
                break;
            case "xm":
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.homeapp_item_hw,viewGroup,false);
                break;
            default:
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.homeapp_item,viewGroup,false);
                break;
        }
        MyHolder myHolder=new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder myHolder, final int i) {

        myHolder.CheckBox1.setOnCheckedChangeListener(null);
        myHolder.CheckBox1.setTag(i);
        myHolder.CheckBox1.setText(i+1+"."+ globalapps.get(i).getZname());
        myHolder.CheckBox1.setChecked(globalapps.get(i).IsCheck);
        myHolder.CheckBox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!globalapps.get(i).isInstall()){
                    ToastUtils.getInstance().shownow(globalapps.get(i).zname+"未安装");
                    myHolder.CheckBox1.setChecked(false);
                    globalapps.get(i).setCheck(false);
                }else {
                    myHolder.CheckBox1.setChecked(isChecked);
                    globalapps.get(i).setCheck(isChecked);
                }
            }
        });
        if (checkBoxList.size()<globalapps.size()){
            checkBoxList.add(myHolder.CheckBox1);
            Log.v("添加",globalapps.get(i).getZname());
        }

    }

    @Override
    public int getItemCount() {
        return globalapps.size();
    }

    class MyHolder extends RecyclerView.ViewHolder{
        CheckBox CheckBox1;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            CheckBox1=itemView.findViewById(R.id.CheckBox1);
        }
    }
}
